<?php

/**
 * Created on 7 mai 2011
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class Coordinates implements iCRUD {
    /*
     * Attributes
     */

    private $_coordinateSetId;
    private $_atomID;
    private $_xCoordinate;
    private $_yCoordinate;


	public function Coordinates(){
		$this->_atomID = null;
        $this->_xCoordinate = null;
        $this->_yCoordinate = null;
        $this->_coordinateSetId = -1;
	}

    /*
     * Constructor
     */	
    public function __Coordinates($atomID, $xCoordinate, $yCoordinate, $coordinateSetId=-1) {
        $this->_atomID = $atomID;
        $this->_xCoordinate = $xCoordinate;
        $this->_yCoordinate = $yCoordinate;
        $this->_coordinateSetId = $coordinateSetId;
    }

    /*
     * Accessors
     */

    public function getAtomId() {
        return $this->_atomID;
    }

    public function getcoordinateSetId() {
        return $this->_coordinateSetId;
    }

    public function getxCoordinate() {
        return $this->_xCoordinate;
    }

    public function getyCoordinate() {
        return $this->_yCoordinate;
    }

    /*
     * Modifiers
     */

    public function setAtomId($param) {
        $this->_atomID = $param;
    }

    public function setcoordinateSetId($param) {
        $this->_coordinateSetId = $param;
    }

    public function setxCoordinate($param) {
        $this->_xCoordinate = $param;
    }

    public function setyCoordinate($param) {
        $this->_yCoordinate = $param;
    }

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {
        $connection=Database::connect();
        if ($this->_coordinateSetId != -1)
            die("Unable to create, still existing!");
        else {
            $query = "INSERT INTO `COORDINATE_SET_2D` values (NULL,'" .
                    $this->_atomID . "','" .
                    $this->_xCoordinate . "','" .
                    $this->_yCoordinate . "')";
            mysql_query($query) or die(mysql_error());
            $this->_coordinateSetId = mysql_insert_id();
        }
        Database::disconnect($connection);
    }
    
    
    public static function createFromArray($arrayOfCoordinates){
    	$connection=Database::connect();
//    	var_dump("</br></br>");
    	foreach($arrayOfCoordinates as $coordinate){
    	if ($coordinate->_coordinateSetId != -1)
            die("Unable to create, still existing!");
        else {
//        	var_dump("creation of the coordinates: ".$coordinate->_atomID." --> ".$coordinate->_xCoordinate." ; ".$coordinate->_yCoordinate."</br>");
            $query = "INSERT INTO `COORDINATE_SET_2D` values (NULL,'" .
                    $coordinate->_atomID . "','" .
                    $coordinate->_xCoordinate . "','" .
                    $coordinate->_yCoordinate . "')";
            mysql_query($query) or die(mysql_error());
	        }
    	}
    	Database::disconnect($connection);
    }
    
    

    /**
     * Read an object from the database
     */
    public static function read($key) {
        $connection=Database::connect();
        $query = "SELECT * FROM COORDINATE_SET_2D WHERE bond_id = '" . $key[0] .
                "' AND coordinate_set_id = '" . $key[1] . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        $coordinates = new Coordinates();
        $coordinates->__Coordinates($x_coordinate, $y_coordinate, $coordinate_set_id, $atom_id);
		Database::disconnect($connection);
        return $coordinates;
    }

    /**
     * Update an object from the database
     */
    public function update() {
        $connection=Database::connect();
        $query = "UPDATE `COORDINATE_SET_2D` SET x_coordinate='" .
                $this->_xCoordinate . "', y_coordinate='" .
                $this->_yCoordinate . "' WHERE coordinate_set_id='" .
                $this->_coordinateSetId . "' AND atom_id='" .
                $this->_atomID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

    /**
     * Delete an object from the database
     */
    public function delete() {
        $connection=Database::connect();
        $query = "DELETE FROM `COORDINATE_SET_2D` WHERE coordinate_set_id='" .
                $this->_coordinateSetId . "' AND atom_id='" .
                $this->_atomID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

}
?>
